package cn.bugstack.infrastructure.persistent.repository;

import cn.bugstack.domain.strategy.model.entity.StrategyAwardEntity;
import cn.bugstack.domain.strategy.repository.IStrategyRepository;
import cn.bugstack.infrastructure.persistent.dao.IStrategyAwardDao;
import cn.bugstack.infrastructure.persistent.po.StrategyAward;
import cn.bugstack.infrastructure.persistent.redis.IRedisService;
import cn.bugstack.infrastructure.persistent.redis.RedissonService;
import cn.bugstack.types.common.Constants;
import org.redisson.api.RMap;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Title: StrategyRepository
 * @Author lz
 * @Date 2025/7/23 20:55
 * @description: 策略仓库接口实现
 */
@Repository
public class StrategyRepository implements IStrategyRepository {
    @Resource
    private IStrategyAwardDao strategyAwardDao;
    @Resource
    private IRedisService redisService;
    @Override
    public List<StrategyAwardEntity> queryStrategyAwardList(Long strategyId) {
        // 从redis中获取
        String cacheKey = Constants.RedisKey.STRATEGY_AWARD_KEY + strategyId;
        List<StrategyAwardEntity> strategyAwardEntity = redisService.getValue(cacheKey);
        if (strategyAwardEntity == null || strategyAwardEntity.isEmpty() ){
            //redis中没有，从数据库中获取
            List<StrategyAward> strategyAwards = strategyAwardDao.queryStrategyAwardListById(strategyId);
            //上次那个表置空
            strategyAwardEntity=new ArrayList<>(strategyAwards.size());
            for (StrategyAward strategyAward : strategyAwards){
                StrategyAwardEntity entity=StrategyAwardEntity.builder()
                        .strategyId(strategyAward.getStrategyId())
                        .awardId(strategyAward.getAwardId())
                        .awardCount(strategyAward.getAwardCount())
                        .awardCountSurplus(strategyAward.getAwardCountSurplus())
                        .awardRate(strategyAward.getAwardRate())
                        .build();
                strategyAwardEntity.add(entity);
            }
            redisService.setValue(cacheKey,strategyAwardEntity);
            return strategyAwardEntity;

        }
        else return strategyAwardEntity;
    }

    @Override
    public void storeStrateAwardSearchTable(Long strategyId, Integer rateRange, Map<Integer, Integer> suffleStrategyRateTables) {
        redisService.setValue(Constants.RedisKey.STRATEGY_RATE_RANGE_KEY+strategyId,rateRange);//存储策略id和概率区间
        //存储概率查找表
        Map<Integer, Integer> cacheRateTable = redisService.getMap(Constants.RedisKey.STRATEGY_RATE_TABLE_KEY + strategyId);
        cacheRateTable.putAll(suffleStrategyRateTables);

    }

    @Override
    public Integer getRateRange(Long strategyId) {
       return redisService.getValue(Constants.RedisKey.STRATEGY_RATE_RANGE_KEY+strategyId);
    }

    @Override
    public Integer getStrategyAwardAssemble(Long strategyId, int i) {
        return redisService.getFromMap(Constants.RedisKey.STRATEGY_RATE_TABLE_KEY+strategyId,i);
    }
}
